package com.xlh.enums.user;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;
import java.util.Objects;

/**
 * 身份枚举
 * <p>
 * Created by lx on 2019/2/24 0024.
 */
@Getter
@AllArgsConstructor
public enum IdentityEnum {

    TEACHER(1, "教师") {
        @Override
        public List<Integer> getRoles() {
            return Lists.newArrayList(RoleEnum.TEACHER.getRoleCode(),
                    RoleEnum.UNIVERSITY_TEACHER.getRoleCode(),
                    RoleEnum.MARKET_TEACHER.getRoleCode());
        }

        @Override
        public List<Integer> getReleaseVersionRoles() {
            return Lists.newArrayList(RoleEnum.TEACHER.getRoleCode());
        }
    },
    STUDENT(2, "学生") {
        @Override
        public List<Integer> getRoles() {
            return Lists.newArrayList(RoleEnum.STUDENT.getRoleCode());
        }

        @Override
        public List<Integer> getReleaseVersionRoles() {
            return Lists.newArrayList(RoleEnum.STUDENT.getRoleCode());
        }
    },
    ADMIN(3, "管理员") {
        @Override
        public List<Integer> getRoles() {
            return Lists.newArrayList(RoleEnum.ADMIN.getRoleCode(),
                    RoleEnum.SUPER_ADMIN.getRoleCode(),
                    RoleEnum.INTERNET_ADMIN.getRoleCode());
        }

        @Override
        public List<Integer> getReleaseVersionRoles() {
            return Lists.newArrayList(RoleEnum.ADMIN.getRoleCode(),
                    RoleEnum.SUPER_ADMIN.getRoleCode());
        }
    };

    private Integer roleCode;

    private String roleName;

    /**
     * 获取相应角色
     *
     * @return
     */
    public abstract List<Integer> getRoles();

    /**
     * 获取正常对外部署时的角色
     *
     * @return
     */
    public abstract List<Integer> getReleaseVersionRoles();

    public static IdentityEnum getByCode(Integer code) {
        for (IdentityEnum identityEnum : IdentityEnum.values()) {
            if (Objects.equals(identityEnum.getRoleCode(), code)) {
                return identityEnum;
            }
        }
        return null;
    }

    public static IdentityEnum getByRole(Integer roleId){
        for (IdentityEnum identityEnum : IdentityEnum.values()) {
            if (identityEnum.getRoles().contains(roleId)) {
                return identityEnum;
            }
        }
        return null;
    }

}
